LINQ to XML - Load Samples


LINQ, XML
LINQ to XML
Desktop, Data
en-US
7/8/2011

Introduction

These samples show how to load an XML document and run queries against it.

Building the Sample

  1. Open Program.cs
  2. Uncomment the line for the sample that you want to run; comment ones you don't want to run
  3. Press Ctrl + F5

Screenshot

Sample Code

C#
Edit|Remove
            public void XLinq5() 
            { 
                XmlReader reader = XmlReader.Create("config.xml"); 
                //the file has comments and whitespace at the start 
                reader.Read(); 
                reader.Read(); 
                XElement config = new XElement("appSettings", 
                                               "This content will be replaced"); 
                config.RemoveAll(); 
                while (!reader.EOF) 
                    config.Add(XNode.ReadFrom(reader)); 
                Console.WriteLine(config); 
            }

Source Code Files

More Information

For more information on LINQ to XML: http://msdn.microsoft.com/en-us/library/bb387098.aspx